package com.pf.service.impl;

import com.google.common.collect.Lists;
import com.pf.dto.User;
import com.pf.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/12/26 20:41
 */
@Service
@Slf4j
public class CacheServiceImpl implements CacheService {

    @Override
    @Cacheable(value = "userCache")
    public List<User> getAllUser() {
        log.info(">>>>> getAllUser...");
        User u1 = User.builder().userId(1).userName("张三").build();
        User u2 = User.builder().userId(2).userName("李四").build();
        User u3 = User.builder().userId(3).userName("王五").build();
        return Lists.newArrayList(u1, u2, u3);
    }

    @Override
    @Cacheable(value = "userCache", key = "#userId")
    public User getUserById(Integer userId) {
        log.info(">>>>> getUserById...");
        return User.builder().userId(userId).userName("张三").build();
    }

    @Override
    @CachePut(value = "userCache", key = "#userId")
    public User addUser(User user) {
        log.info(">>>>> addUser...");
        return user;
    }

    @Override
    @CachePut(value = "userCache", key = "#userId")
    public User updateById(User user) {
        log.info(">>>>> updateById...");
        return user;
    }

    @Override
    @CacheEvict(value = "userCache", key = "#userId")
    public void deleteById(Integer userId) {
        log.info(">>>>> deleteById...");
    }

}
